import Vue from "vue";
import { Toast } from "vant";

Vue.use(Toast);

export default {
  AddToCart(state, goodsData) {
    const list = state.cart.some(item => item.id === goodsData.id);
    if (list) {
      state.cart = state.cart.map(item => {
        if (item.id === goodsData.id) item.amount += goodsData.amount;
        return item;
      });
    } else {
      state.cart = [...state.cart, { ...goodsData, isChecked: false }];
    }
    Toast("已加入购物车");
  },
  //删除单个商品
  removeFromCart(state, { id }) {
    // console.log(id);
    state.cart = state.cart.filter(item => item.id !== id);
  },
  //删除选中商品
  deleteChecked(state) {
    state.cart = state.cart.filter(item => item.isChecked === false);
  },
  //全选
  checkAll(state, { checked }) {
    state.cart = state.cart.map(item => {
      item.isChecked = checked;
      return item;
    });
  },
  //修改数量
  modifyCartNum(state, { id, num }) {
    state.cart = state.cart.map(item => {
      if (item.id === id) {
        item.amount = num;
      }
      return item;
    });
  },
  //修改选中数量
  changeCheckedStatus(state, { id }) {
    state.cart = state.cart.map(item => {
      if (item.id === id) {
        item.isChecked = !item.isChecked;
      }
      return item;
    });
  },
  //登陆状态
  changeIsLogin(state, { status }) {
    state.isLogin = status;
  },

  //添加优惠券

  addCoupon(state, couponData) {
    state.couponList = [
      ...state.couponList,
      {
        ...couponData
      }
    ];
    Toast("兑换成功");
  }
};
